<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class simpanan extends CI_Controller {

    public function __construct() {
        parent::__construct();

        $this->load->library('auth');
        $this->load->library('base');
        $this->load->library('ajaxresponse');
        $this->load->library("phpexcel");
        $this->load->library("PHPExcel/iofactory");

        $this->load->model("report/m_simpanan");
        $this->load->model("crud/m_info_usaha");
    }

    function index() {
        $this->simpanan();
    }

    function simpanan(){
        $this->auth->is_logged_in('form/login');
        $this->base->head_html('Laporan Simpanan Anggota');
        $data["info_usaha"] = $this->m_info_usaha->get()->result();
        $this->load->view('pengurus/br-08/akuntansi/jurnal', $data);
    }

    function calc_simpanan(){
        $date = $this->input->post("TGL_JURNAL");
        $result = $this->m_akuntansi->sum_jurnal($date);

        // --------------------------- create excel ---------------------------------//
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setTitle("Jurnal")->setDescription("description");

        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(false);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);

        $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(1);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0.75);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.75);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(1);

        $objPHPExcel->getActiveSheet()->getStyle('A8')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
        $objPHPExcel->getActiveSheet()->getStyle('A8')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A8')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('A8'), 'B8:H8' );

        $objPHPExcel->getActiveSheet()->setCellValue('A1', "KOPERASI KARYAWAN TOTAL INDONESIE");
        $objPHPExcel->getActiveSheet()->setCellValue('A2', "JL.JEND.SUDIRMAN H2 NO.5-6 BALIKPAPAN PERMAI");
        $objPHPExcel->getActiveSheet()->setCellValue('A3', "BALIKPAPAN");
        $objPHPExcel->getActiveSheet()->setCellValue('A4', "Tlp.: 0542-427723, Fac.: 0542-427722");
        $sheet = $objPHPExcel->getActiveSheet();
        $sheet->mergeCells('A1:H1');
        $sheet->mergeCells('A2:H2');
        $sheet->mergeCells('A3:H3');
        $sheet->mergeCells('A4:H4');
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        $objPHPExcel->getActiveSheet()->setCellValue('A6', "JURNAL BULAN : Oktober 2010");
        $objPHPExcel->getActiveSheet()->setCellValue('H6', "TANGGAL : 10-02-2012");

        $objPHPExcel->getActiveSheet()->setCellValue('A8', "No");
        $objPHPExcel->getActiveSheet()->setCellValue('B8', "Tanggal");
        $objPHPExcel->getActiveSheet()->setCellValue('C8', "Nomer Transaksi");
        $objPHPExcel->getActiveSheet()->setCellValue('D8', "No.Perkiraan");
        $objPHPExcel->getActiveSheet()->setCellValue('E8', "Perkiraan");
        $objPHPExcel->getActiveSheet()->setCellValue('F8', "Debet");
        $objPHPExcel->getActiveSheet()->setCellValue('G8', "Kredit");
        $objPHPExcel->getActiveSheet()->setCellValue('H8', "Keterangan");

        $i = 8;
        $notrx = 0;
        foreach($result->result() as $row) {
                $i++;
                $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $i-8);
                $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $row->TGL_TRANSAKSI);
                $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $row->NOMER_TRANSAKSI);
                $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $row->ID_PERKIRAAN);
                $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $row->URAIAN_PERKIRAAN);
                if ($row->KODE=='D'){
                    $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $row->JUMLAH);
                }
                else if ($row->KODE=='K'){
                    $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $row->JUMLAH);
                }
                $objPHPExcel->getActiveSheet()->setCellValue('H' . $i, $row->KETERANGAN);
        }
        //$objPHPExcel->getActiveSheet()->setCellValue("E$i", "=SUM(E2:E".$i.")");
        //$objPHPExcel->getActiveSheet()->setCellValue("F$i", "=SUM(F2:F".$i.")");
        //$objPHPExcel->getActiveSheet()->setCellValue("G$i", "=SUM(G2:G".$i.")");
        $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&G&C&HLaporan Jurnal\n!');
        $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');

        // Save it as an excel 2003 file
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=Laporan Jurnal_$date");
        header('Cache-Control: max-age=0');

        $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
    }

    function calc_rugilaba()
    {
        $date = $this->input->post("TGL_RUGI_LABA");

        $this->load->model('crud/m_info_usaha');

        $this->load->model('crud/m_info_usaha');

        $info_usaha = $this->info_usaha->get();

        foreach($info_usaha->result() as $row)
        {
            
        }
        
        $result = $this->m_akuntansi->sum_jurnal($date);
        $datestring = "%d-%M-%Y";
        $todate = mdate($datestring, time());


        // --------------------------- create excel ---------------------------------//
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setTitle("Jurnal")->setDescription("description");

        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(false);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);


        $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(1);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0.75);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.75);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(1);

        $objPHPExcel->getActiveSheet()->getStyle('A8')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
        $objPHPExcel->getActiveSheet()->getStyle('A8')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A8')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('A8'), 'B8:H8' );

        $objPHPExcel->getActiveSheet()->setCellValue('A1', "KOPERASI KARYAWAN TOTAL INDONESIE");
        $objPHPExcel->getActiveSheet()->setCellValue('A2', "JL.JEND.SUDIRMAN H2 NO.5-6 BALIKPAPAN PERMAI");
        $objPHPExcel->getActiveSheet()->setCellValue('A3', "BALIKPAPAN");
        $objPHPExcel->getActiveSheet()->setCellValue('A4', "Tlp.: 0542-427723, Fac.: 0542-427722");
        $sheet = $objPHPExcel->getActiveSheet();
        $sheet->mergeCells('A1:H1');
        $sheet->mergeCells('A2:H2');
        $sheet->mergeCells('A3:H3');
        $sheet->mergeCells('A4:H4');
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);



        $objPHPExcel->getActiveSheet()->setCellValue('A6', "JURNAL BULAN : $date");
        $objPHPExcel->getActiveSheet()->setCellValue('H6', "TANGGAL : $todate");


        $objPHPExcel->getActiveSheet()->setCellValue('A8', "No");
        $objPHPExcel->getActiveSheet()->setCellValue('B8', "Tanggal");
        $objPHPExcel->getActiveSheet()->setCellValue('C8', "Nomer Transaksi");
        $objPHPExcel->getActiveSheet()->setCellValue('D8', "No.Perkiraan");
        $objPHPExcel->getActiveSheet()->setCellValue('E8', "Perkiraan");
        $objPHPExcel->getActiveSheet()->setCellValue('F8', "Debet");
        $objPHPExcel->getActiveSheet()->setCellValue('G8', "Kredit");
        $objPHPExcel->getActiveSheet()->setCellValue('H8', "Keterangan");

        $i = 8;
        $j = 0;
        $notrx = 0;
        foreach($result->result() as $row) {
            $i++;
            if ($notrx!=$row->NOMER_TRANSAKSI){
                $j++;
                $notrx = $row->NOMER_TRANSAKSI;
                $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $j);
                $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $row->TGL_TRANSAKSI);
                $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $row->NOMER_TRANSAKSI);
                $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $row->ID_PERKIRAAN);
                $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $row->URAIAN_PERKIRAAN);
                if ($row->KODE=='D'){
                    $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $row->JUMLAH);
                }
                else if ($row->KODE=='K'){
                    $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $row->JUMLAH);
                }
                $objPHPExcel->getActiveSheet()->setCellValue('H' . $i, $row->KETERANGAN);
            }else
            {
                $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $row->ID_PERKIRAAN);
                $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $row->URAIAN_PERKIRAAN);
                if ($row->KODE=='D'){
                    $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $row->JUMLAH);
                }
                else if ($row->KODE=='K'){
                    $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $row->JUMLAH);
                }
                //$objPHPExcel->getActiveSheet()->setCellValue('H' . $i, $row->KETERANGAN);
            }
        }
        //$objPHPExcel->getActiveSheet()->setCellValue("E$i", "=SUM(E2:E".$i.")");
        //$objPHPExcel->getActiveSheet()->setCellValue("F$i", "=SUM(F2:F".$i.")");
        //$objPHPExcel->getActiveSheet()->setCellValue("G$i", "=SUM(G2:G".$i.")");
        $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&G&C&HLaporan Jurnal\n!');
        $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');

        // Save it as an excel 2003 file
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="Laporan Jurnal.xls"');
        header('Cache-Control: max-age=0');

        $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
    }

}
?>
